<template>
  <span>
    {{ optionMap[value] || value }}
  </span>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue';
import { ICellRendererParams } from 'ag-grid-community';
import OptionVO from '@/bean/vo/OptionVO';
import AgColumn from '@/bean/vo/AgColumn';


const props = defineProps<{
  params: ICellRendererParams;
}>();

const options = ref<OptionVO[]>(getOptions(props.params.colDef.cellRendererParams));

const optionMap = computed(() => {
  return Object.fromEntries(options.value.map(({label, value}) => [value, label]));
});

const value = ref(props.params.value);

function getOptions(optionsFunc: AgColumn<any>['options']) {
  const options = typeof optionsFunc === 'function' ? optionsFunc(props.params) : optionsFunc;
  return options || [];
}

function getValue() {
  return value.value;
}

defineExpose({
  getValue
});
</script>
